import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import *as API from '@/api/http'
// import type * as crmTS from './crmTS'

let initialState={
    shoplist:[],
    typelist:[],
    activeKey:'1'
}
const todosSlice = createSlice({
  name: "todos",
  initialState,
  reducers: {
    t_getshoplist(state, action) {//获取
    state.shoplist=action.payload
    },
    t_gettypelist(state,action){//获取
    state.typelist=action.payload
    },
    t_addshoplist(state,action){//添加
    state.shoplist.push(action.payload)
    },
    t_editshoplist(state,action){//编辑
    let i=state.shoplist.findIndex(item=>{
        return item.id==action.payload.id
    })
    state.shoplist[i]=action.payload
    },
    t_delshoplist(state,action){//删除
    state.shoplist=state.shoplist.filter
    (item=>item.id!==action.payload)
    },
    
  },
});

export const actionfn = todosSlice.actions;
export default todosSlice.reducer;

//异步
export let async_getshoplist=createAsyncThunk('async_getshoplist',
    async(_,store)=>{
        let res=await API.http_getshoplist()
        console.log(res);
        
        store.dispatch(actionfn.t_getshoplist(res))
})
export let async_gettypelist=createAsyncThunk('async_gettypelist',
    async(_,store)=>{
        let res=await API.http_gettypelist()
        store.dispatch(actionfn.t_gettypelist(res))
})
export let async_addshoplist=createAsyncThunk('async_addshoplist',
    async(obj,store)=>{
        let res=await API.http_addshoplist(obj)
        store.dispatch(actionfn.t_addshoplist(res))
})
export let async_editshoplist=createAsyncThunk('async_editshoplist',
    async(obj,store)=>{
        let res=await API.http_editshoplist(obj)
        store.dispatch(actionfn.t_editshoplist(res))
})
export let async_delshoplist=createAsyncThunk('async_delshoplist',
    async(id,store)=>{
        await API.http_delshoplist(id)
        store.dispatch(actionfn.t_delshoplist(id))
})
export let async_searchshoplist=createAsyncThunk('async_searchshoplist',
    async(title,store)=>{
        let res=await API.http_searchshoplist(title)
        store.dispatch(actionfn.t_getshoplist(res))
})
